package com.ciwong.mobilelib.widget.listview;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ListView;

import com.ciwong.libs.utils.CWLog;

/**
 * 滑动删除的ListView
 *
 * 说明需要配合SlideView作为Item布局使用 使用方法参照
 *
 * @author res-jianpingliu
 *
 */
public class SlideListView extends ListView implements ISlideListView
{
    /**
     * 当前选中的item
     */
    private SlideView mSelectedView;

    /**
     * 上次操作的item
     */
    private SlideView mLastSlideView;

    /**
     * 是否为滑动开启状态
     */
    private int mSlideStatus;

    /**
     * 阀门值
     */
    private int mTouchSlop;

    /**
     * 是否滑动了
     */
    private boolean moved;

    /**
     * 手指按下时的y坐标
     */
    private int mLastY;

    /**
     * 两次点击时间间隔
     */
    private long duration = 1000;

    private int canClick = 1;

    private boolean clickAble = true;

    private boolean hasSend;

    private Handler handler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            int tag = msg.what;
            if (tag == canClick)
            {
                clickAble = true;
                hasSend = false;
            }
        }
    };

    /**
     * 滑动删除的ListView
     *
     * 说明需要配合SlideView作为Item布局使用
     *
     * @param context
     *            上下文
     * @@param attrs 属性
     *
     */
    public SlideListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        final ViewConfiguration configuration = ViewConfiguration
                .get(getContext());
        mTouchSlop = configuration.getScaledTouchSlop();
        // CWLog.e("ljp", "mTouchSlop = " + mTouchSlop);
    }

    /**
     * 还原指定项
     *
     * @param position
     *            索引
     */
    public void shrinkListItem(int position)
    {
        View item = getChildAt(position);

        if (item != null)
        {
            try
            {
                ((SlideView) item).shrink();
            }
            catch (ClassCastException e)
            {
                // e.printStackTrace();
                CWLog.e("ljp", "shrinkListItem Exception");
            }
        }
    }

    /**
     * 关闭上次操作
     *
     */
    public void shrink()
    {
        if (mLastSlideView != null)
        {
            mLastSlideView.shrink();
        }
    }

    public void setLastSlideView(SlideView slideView)
    {
        mLastSlideView = slideView;
    }

    /**
     * 获取上一次操作的item
     *
     * @return SlideView
     */
    public SlideView getLastSlideView()
    {
        return mLastSlideView;
    }

    /**
     * 设置当前操作的item
     *
     * @param slideView
     *            当前操作的item
     */
    public void setSelectedSlideView(SlideView slideView)
    {
        mSelectedView = slideView;
    }

    public SlideView getSelectedSlideView()
    {
        return mSelectedView;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        if (ev.getAction() == MotionEvent.ACTION_UP)
        {
            if (!clickAble && getSlideStatus() == SlideView.OnSlideListener.SLIDE_STATUS_OFF)
            {
                sendMsg();
                return true;
            }
            sendMsg();
        }

        return super.onInterceptTouchEvent(ev);
    }

    private void sendMsg()
    {
        if (!hasSend)
        {
            clickAble = false;
            hasSend = true;
            handler.sendEmptyMessageDelayed(canClick, duration);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev)
    {
        int x = (int) ev.getX();
        int y = (int) ev.getY();

        switch (ev.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                // CWLog.e("ljp", "listview ACTION_DOWN ...");

                mLastY = y;
                int position = pointToPosition(x, y);
                if (position != INVALID_POSITION)
                {
                    View child = getChildAt(position
                            - getFirstVisiblePosition());
                    if (child != null)
                    {
                        SlideView.SlideItem slideItem = (SlideView.SlideItem) child.getTag();
                        if (slideItem != null)
                        {
                            mSelectedView = slideItem.getSlideView();

                            if (mSelectedView != null)
                            {
                                mSelectedView.setSelectPosition(position);
                                if (getSlideStatus() != SlideView.OnSlideListener.SLIDE_STATUS_OFF
                                        && mLastSlideView != null
                                        && mLastSlideView != mSelectedView)
                                {
                                    mLastSlideView.shrink();
                                    mLastSlideView = null;
                                    return false;
                                }
                                mLastSlideView = mSelectedView;
                            }
                            // if the down position on the header, close the
                            // slideView
                            else
                            {
                                // if the mLastSlideView's status is STATUS_ON,
                                // do not
                                // dispatch event
                                if (mLastSlideView != null
                                        && mLastSlideView.getStatus() == SlideView.OnSlideListener.SLIDE_STATUS_ON)
                                {
                                    mLastSlideView.shrink();
                                    mLastSlideView = null;
                                    return false;
                                }
                            }
                        }
                        else
                        {
                            //如果SlideItem等于null，那么表示event不是落在item上，如果SLIDE_STATUS_ON状态，那么拦截事件，关闭mLastSlideView
                            if (mSlideStatus == SlideView.OnSlideListener.SLIDE_STATUS_ON && mLastSlideView != null)
                            {
                                mLastSlideView.shrink();
                                return false;
                            }
                        }
                    }
                }
                else
                {
                    if (mLastSlideView != null
                            && mSlideStatus == SlideView.OnSlideListener.SLIDE_STATUS_ON)
                    {
                        mLastSlideView.shrink();
                        mLastSlideView = null;
                        return false;
                    }
                }
                break;

            }
            case MotionEvent.ACTION_MOVE:
            {

                int detaY = Math.abs(y - mLastY);
                // CWLog.e("ljp", "listview ACTION_MOVE ... status = "
                // + mSelectedView.getStatus() + " mTouchSlop = "
                // + mTouchSlop + " detaX = " + detaX + " , detaY = "
                // + detaY);
                if (mSelectedView != null
                        && mSelectedView.getStatus() != SlideView.OnSlideListener.SLIDE_STATUS_OFF
                        && detaY > mTouchSlop)
                {
                    mSelectedView.upScrollTo();
                    moved = true;
                    return false;
                }

                break;
            }
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            {
                // CWLog.e("ljp", "listview ACTION_UP ...");
                if (moved)
                {
                    moved = false;
                    return true;
                }

                break;
            }
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 获取滑动状态
     *
     * @return 状态
     */
    public synchronized int getSlideStatus()
    {
        return mSlideStatus;
    }

    /**
     * 设置滑动状态
     *
     * @param slide
     */
    public synchronized void setSlideStatus(int slide)
    {
        // CWLog.e("ljp", "listview setSlideStatus ... " + slide);
        mSlideStatus = slide;
    }

}